package com.lx.constant.segment.common;

import cn.hutool.core.util.StrUtil;
import com.lx.common.utils.CommonClassUtil;
import com.lx.constant.consts.CommonConstant;
import com.lx.constant.enums.ConditionKeyWordEnum;
import com.lx.constant.segment.ChildConditionSegment;
import com.lx.constant.segment.Segment;
import com.lx.wrapper.AbstractWrapper;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.query.Query;

import javax.persistence.Column;
import javax.persistence.Id;
import java.lang.reflect.Field;
import java.util.Objects;

/**
 * 条件语句
 * 1.WHERE
 * 2.HAVING
 *
 * @author lx
 * @create 2022/10/30 10:45
 **/
@Getter
@Setter
public class ConditionSegment extends ChildConditionSegment {

    /**
     * 条件关键字
     */
    private ConditionKeyWordEnum conditionKeyWord;

    /**
     * 条件对应实体类型
     */
    private Class<?> conditionEntityClass;

    /**
     * 条件属性
     */
    private Field conditionField;

    /**
     * 条件列属性值
     */
    private Object[] columnValue;

    @Override
    public String convertSegmentToQL(AbstractWrapper parentWrapper, AbstractWrapper thisWrapper, boolean isHQL) {
        ConditionKeyWordEnum ww = getConditionKeyWord();
        return ww.getConditionGenerator().generatorCondition(parentWrapper, thisWrapper, ww.getFormatEl(), this, isHQL);
    }

    @Override
    public String getHibernatePlaceholder(boolean needAddColon) {
        //添加条件关键字到别名
        return super.getHibernatePlaceholder(needAddColon) + getConditionKeyWord().name().substring(0, 2);
    }

    @Override
    public boolean needJoinSep(ConditionKeyWordEnum joinSep, Segment prevSegment) {
        if (getConditionKeyWord().getGrade() <= joinSep.getGrade()) {
            //如果传入关键字同级别
            return false;
        }

        if (prevSegment instanceof ConditionSegment) {
            ConditionSegment prevCon = (ConditionSegment) prevSegment;
            if (Objects.isNull(prevCon.getChildConditionWrapper()) && prevCon.getConditionKeyWord().getGrade() == joinSep.getGrade()) {
                //如果不存在条件或条件最后一个值为AND同级别的语句.
                return false;
            }
        }
        return true;
    }

    @Override
    public void setHibernateParamByQuery(boolean isHQL, Query query) {
        super.setHibernateParamByQuery(isHQL, query);
        if (getChildConditionWrapper() == null) {
            //对应的条件关键字设置解析参数
            conditionKeyWord.getConditionGenerator().setHibernateParam(this, isHQL, query);
        }
    }

    @Override
    public String getColumnNameBySegment(boolean isHQL) {
        String originColumnName = super.getColumnNameBySegment(isHQL);
        if (StrUtil.isEmpty(originColumnName)) {
            return originColumnName;
        }
        if (!isHQL) {
            //SQL获取的列名则直接返回
            return originColumnName;
        }
        //add by lx Date:2023/2/26 Desc: 支持hibernate特殊语句写法的扩展.例:如遇 User user[@Id@Column String id] =>则Hql解析为 user.id
        Field columnField = getColumnField();
        if (columnField == null) {
            return originColumnName;
        }
        Field idColumnField = CommonClassUtil.findAnnotationFieldFromClass(columnField.getType(), Id.class, Column.class);
        if (idColumnField == null) {
            return originColumnName;
        }
        return originColumnName + CommonConstant.POINT + idColumnField.getAnnotation(Column.class).name();
    }

}
